﻿
namespace StockModel
{
    /// <summary>
    /// 用于记录数据股票历史数据。
    /// </summary>
    public class StockData
    {
        public string file = @"C:\gitee.com\stock_research\samples\sh.600749.csv";

        public List<StockTradePlan> plans = new List<StockTradePlan>();

        public List<StockDayKLine> kdata = new List<StockDayKLine>();

        public StockData() { }
        public StockData(string file) { Load(file); }

        public void Load(string file)
        {
            this.file = file;
            kdata = read_csv(file);
        }


        /// <summary>
        /// 读取csv数据。
        /// </summary>
        /// <param name="filename">待读取文件。</param>
        /// <returns></returns>
        public static List<StockDayKLine> read_csv(string filename)
        {
            // ,date,preclose,high,low,close
            // 0,2000 - 01 - 04,11.5400,11.8800,11.5000,11.7600
            // 1,2000 - 01 - 05,11.7600,11.9500,11.6200,11.7000
            List<StockDayKLine> sinfo = new List<StockDayKLine>();
            var lines = File.ReadAllLines(filename);
            foreach (var line in lines)
                if (StockDayKLine.parse(line) is StockDayKLine dk)
                    sinfo.Add(dk);
            return sinfo;
        }

        public static void Statictics(string filename)
        {

        }

        /// <summary>
        /// 0=sum, 1=avg, 2=x2, 3=x2p
        /// </summary>
        /// <param name="data"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static double[] Statictics(List<StockDayKLine> data, int year)
        {
            double[] res = new double[5];
            double sum = 0;
            double average = 0;
            double x2 = 0;
            var vs = new List<StockDayKLine>();
            foreach (var v in data)
                if (v.date.Year == year)
                    vs.Add(v);
            foreach (var v in vs)
                sum += v.close;

            average = sum / vs.Count;

            double tmp = 0;
            foreach (var v in vs)
                tmp += Math.Pow(v.close - average, 2);
            x2 = Math.Sqrt(tmp);
            double x2p = x2 / average;
            res[0] = sum;
            res[1] = average;
            res[2] = x2;
            res[3] = x2p;
            return res;

        }
    }
}
